package lg.minecraft.plugin.pokemonmenu.gui;

import com.cobblemon.mod.common.api.moves.Move;
import com.cobblemon.mod.common.api.moves.MoveSet;
import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.PokemonPlayer;
import lg.minecraft.plugin.pokemonmenu.manager.PokemonPlayerManger;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 * @author langle__
 * @version 1.0
 */
public class ViewPokemonInventory extends LGInventory {

    private static final int[] GLASS_ARRAY = {45, 46, 47, 48, 50, 51, 52, 53};

    private static final int[] MOVE_INDEX = {19, 21, 23, 25};

    private final Pokemon pokemon;

    public ViewPokemonInventory(Pokemon pokemon) {
        super(6, LGPokemonMenu.getMessage().getGuiTitlePokemon());
        this.pokemon = pokemon;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        Player player = context.getPlayer();
        ItemStack pokemonItem = LGPokemonMenu.getGuiItem().getPokemon(pokemon);
        inventory.setItem(4, pokemonItem);

        for (int slot : GLASS_ARRAY) {
            inventory.setItem(slot, LGPokemonMenu.getGuiItem().getBorder());
        }

        if (PokemonPlayerManger.INSTANCE.getPokemonPlayer(player).isPreviousInventoryEmpty()) {
            inventory.setItem(49, LGPokemonMenu.getGuiItem().getBorder());
        } else {
            inventory.setItem(49, LGPokemonMenu.getGuiItem().getBack());
        }

        MoveSet moveSet = pokemon.getMoveSet();
        ItemStack infoMoveNoExist = LGPokemonMenu.getGuiItem().getInfoMoveNoExist();
        for (int i = 0; i < 4; i++) {
            Move move = moveSet.get(i);
            int slot = MOVE_INDEX[i];
            inventory.setItem(slot, move == null ? infoMoveNoExist : LGPokemonMenu.getGuiItem().getInfoMoveExist(move));
        }

        inventory.setItem(0, LGPokemonMenu.getGuiItem().getBaseInfo(pokemon));
        inventory.setItem(2, LGPokemonMenu.getGuiItem().getAbilityInfo(pokemon));
        inventory.setItem(6, LGPokemonMenu.getGuiItem().getMiscInfo(pokemon));
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();
        PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(player);

        if (slot == 49 && !pokemonPlayer.isPreviousInventoryEmpty()) {
            pokemonPlayer.getPreviousInventory().openInventory(player);
            pokemonPlayer.setPreviousInventory(null);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {

    }

}
